// transitions between groups not just puzzle to saver and back.


////////////////////////////////////////////
/// baseline definition
////////////////////////////////////////////

capture drop p04 p08 p12 s04 s08 s12

gen pp=group_a==1 if year==2004
bys case: egen p04=max(pp) 
drop pp

gen pp=group_a==1 if year==2008
bys case: egen p08=max(pp)
drop pp

gen pp=group_a==1 if year==2012
bys case: egen p12=max(pp)
drop pp


gen pp=group_a==2 if year==2004
bys case: egen b04=max(pp) 
drop pp

gen pp=group_a==2 if year==2008
bys case: egen b08=max(pp)
drop pp

gen pp=group_a==2 if year==2012
bys case: egen b12=max(pp)
drop pp

gen pp=group_a==3 if year==2004
bys case: egen n04=max(pp) 
drop pp

gen pp=group_a==3 if year==2008
bys case: egen n08=max(pp)
drop pp

gen pp=group_a==3 if year==2012
bys case: egen n12=max(pp)
drop pp

gen pp=group_a==4 if year==2004
bys case: egen s04=max(pp)
drop pp

gen pp=group_a==4 if year==2008
bys case: egen s08=max(pp)
drop pp

gen pp=group_a==4 if year==2012
bys case: egen s12=max(pp)
drop pp

// puzzle transition to other:

gen p_other08=.
replace p_other08=0 if (p04==1 & p08==1) & year==2008
replace p_other08=1 if (p04==1 & s08==1) & year==2008
replace p_other08=2 if (p04==1 & b08==1) & year==2008
replace p_other08=3 if (p04==1 & n08==1) & year==2008


gen p_other12=.
replace p_other12=0 if (p08==1 & p12==1) & year==2012
replace p_other12=1 if (p08==1 & s12==1) & year==2012
replace p_other12=2 if (p08==1 & b12==1) & year==2012
replace p_other12=3 if (p08==1 & n12==1) & year==2012

gen p_tr=p_other08 if year==2008
replace p_tr=p_other12 if year==2012

drop p_other*

// saver transition to other:

gen s_other08=.
replace s_other08=0 if (s04==1 & s08==1) & year==2008
replace s_other08=1 if (s04==1 & p08==1) & year==2008
replace s_other08=2 if (s04==1 & b08==1) & year==2008
replace s_other08=3 if (s04==1 & n08==1) & year==2008


gen s_other12=.
replace s_other12=0 if (s08==1 & s12==1) & year==2012
replace s_other12=1 if (s08==1 & p12==1) & year==2012
replace s_other12=2 if (s08==1 & b12==1) & year==2012
replace s_other12=3 if (s08==1 & n12==1) & year==2012

gen s_tr=s_other08 if year==2008
replace s_tr=s_other12 if year==2012

drop s_other*
drop p04 p08 p12 b04 b08 b12 n04 n08 n12 s04 s08 s12

////////////////////////////////////////////
/// strict definition
////////////////////////////////////////////

gen pp=group_a5001mb==1 if year==2004
bys case: egen p04a=max(pp) 
drop pp

gen pp=group_a5001mb==1 if year==2008
bys case: egen p08a=max(pp)
drop pp

gen pp=group_a5001mb==1 if year==2012
bys case: egen p12a=max(pp)
drop pp


gen pp=group_a5001mb==2 if year==2004
bys case: egen b04a=max(pp) 
drop pp

gen pp=group_a5001mb==2 if year==2008
bys case: egen b08a=max(pp)
drop pp

gen pp=group_a5001mb==2 if year==2012
bys case: egen b12a=max(pp)
drop pp

gen pp=group_a5001mb==3 if year==2004
bys case: egen n04a=max(pp) 
drop pp

gen pp=group_a5001mb==3 if year==2008
bys case: egen n08a=max(pp)
drop pp

gen pp=group_a5001mb==3 if year==2012
bys case: egen n12a=max(pp)
drop pp

gen pp=group_a5001mb==4 if year==2004
bys case: egen s04a=max(pp)
drop pp

gen pp=group_a5001mb==4 if year==2008
bys case: egen s08a=max(pp)
drop pp

gen pp=group_a5001mb==4 if year==2012
bys case: egen s12a=max(pp)
drop pp

// puzzle transition to other:

gen p_other08=.
replace p_other08=0 if (p04a==1 & p08a==1) & year==2008
replace p_other08=1 if (p04a==1 & s08a==1) & year==2008
replace p_other08=2 if (p04a==1 & b08a==1) & year==2008
replace p_other08=3 if (p04a==1 & n08a==1) & year==2008


gen p_other12=.
replace p_other12=0 if (p08a==1 & p12a==1) & year==2012
replace p_other12=1 if (p08a==1 & s12a==1) & year==2012
replace p_other12=2 if (p08a==1 & b12a==1) & year==2012
replace p_other12=3 if (p08a==1 & n12a==1) & year==2012

capture drop p_tr500
gen p_tr500=p_other08 if year==2008
replace p_tr500=p_other12 if year==2012

drop p_other*

// saver transition to other:

gen s_other08=.
replace s_other08=0 if (s04a==1 & s08a==1) & year==2008
replace s_other08=1 if (s04a==1 & p08a==1) & year==2008
replace s_other08=2 if (s04a==1 & b08a==1) & year==2008
replace s_other08=3 if (s04a==1 & n08a==1) & year==2008


gen s_other12=.
replace s_other12=0 if (s08a==1 & s12a==1) & year==2012
replace s_other12=1 if (s08a==1 & p12a==1) & year==2012
replace s_other12=2 if (s08a==1 & b12a==1) & year==2012
replace s_other12=3 if (s08a==1 & n12a==1) & year==2012

capture drop s_tr500
gen s_tr500=s_other08 if year==2008
replace s_tr500=s_other12 if year==2012

drop s_other*
drop p04 p08 p12 b04 b08 b12 n04 n08 n12 s04 s08 s12

label def s_tr_l 0 "saver to saver" 1 "saver to puzzle" 2 "saver to borrower" 3 "saver to neutral"
label def p_tr_l 0 "puzzle to puzzle" 1 "puzzle to saver" 2 "puzzle to borrower" 3 "puzzle to neutral"

label values s_tr s_tr_l // baseline
label values p_tr p_tr_l

label values s_tr500 s_tr_l  //strict
label values p_tr500 p_tr_l

